<?php
require_once 'AuthController.php';
require_once 'CKEDITOR.php';

class News extends CI_Controller {
	
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'be/NewsModel' );
		$this->load->helper ( 'form' );
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
		$this->load->helper ( 'ckeditor' );
	}
	
	public function index() {
		
		$this->list_news();
	}
	
	function list_news() {
		$auth = new AuthController();
		if($auth->can('view', 'news')) {
			$data ['news'] = $this->NewsModel->getNews ();
			$data ['title'] = 'Tin tức CARDnet';
			$data['page_title'] = 'Danh sách bản tin';
			$data ['main_content'] = 'be/news/views';
			$this->load->view ( 'be/includes/template', $data );
		}else {
			$auth->denied();
		}
	}
	
	public function create() {
		$auth = new AuthController();
		if($auth->can('add', 'news')) {
			$data ['title'] = 'Đăng tin';
			$this->form_validation->set_rules ( 'title', 'Tiêu đề', 'required' );
			$this->form_validation->set_rules ( 'text', 'Nội dung' );
			$data['editor'] = CKEDITOR::createEditor('editor');
			if ($this->form_validation->run () == FALSE) {
				$data ['main_content'] = 'be/news/create';
				$data ['page_title'] = 'Đăng tin';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/news/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/news">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$this->NewsModel->create ();
				$data['page_title'] = 'Đăng tin thành công';
				$mess_detail = '<p>Đăng tin thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/news/add">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/news">Trở lại trang danh sách</a>';
				$data ['message_detail'] = $mess_detail;
				$data ['main_content'] = 'be/message';
			}
			$data['editor'] = CKEDITOR::createEditor();	
			$this->load->view ( 'be/includes/template', $data );
		}else {
			$auth->denied();
		}
	}
	
	public function view($slug) {
		$auth = new AuthController();
		if($auth->can('view', 'news')) {
			$data ['item'] = $this->NewsModel->getNews ( $slug );
			if (empty ( $data ['item'] )) {
				show_404 ();
			}
			$data ['title'] = $data ['item'] ['title'];
			$data ['news'] = $this->NewsModel->getNews ( $slug );
			$data ['main_content'] = 'be/news/item';
			$data['page_title'] = $data['title'];
			$this->load->view ( 'be/includes/template', $data );
		}else {
			$auth->denied();
		}
	}
	
	function delete() {
		$auth = new AuthController();
		if($auth->can('delete', 'news')) {
			$ids = $this->input->post ( 'selected' );
			$flag = true;
			foreach ( $ids as $id ) {
				if (! $this->NewsModel->delete ( $id )) {
					$flag = false;
				}
			}
			if (! $flag) {
				//echo "Delete not successfully!";
			}
			redirect ( site_url ( '/news' ) );
		}
		else {
			$auth->denied();
		}	
	}
	
	function update($id) {
		$auth = new AuthController();
		if($auth->can('update', 'news')) {
			$this->form_validation->set_rules ( 'title', 'Tiêu đề', 'required' );
			$this->form_validation->set_rules ( 'text', 'Nội dung' );
		
			if ($this->input->post('update')) {
				$data ['id'] = $id;
				$data ['title'] = $this->input->post ( 'title' );
				$data ['text'] = $this->input->post ( 'text' );
				$order = array("\r\n", "\n", "\r");
				$replace = "";
				$data['text'] = str_replace($order, $replace, $data['text']);
				
				$result = $this->NewsModel->update ( $id, $data );
				if ($result)
					redirect ( base_url () . 'index.php/news' );
				else
					echo "Can't update this card";
			} else {
				$news_item = $this->NewsModel->get( $id );
				$data ['title'] = $news_item->title;
				$data['text'] = $news_item->text;
				$data['id'] = $id;
				$data['editor'] = CKEDITOR::createEditor('editor');
				$data['main_content'] = 'be/news/update';					
				$this->load->view ( 'be/includes/template', $data );
			}
		}else {
			$auth->denied();
		}
	}
}
